﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using eMeng;
using ValidityCheck;
using JavaScript;
using SQL;

public partial class Admin_ChangePassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void SubmitChanges_Click(object sender, EventArgs e)
    {
        SymmCrypto sc = new SymmCrypto(SymmCrypto.SymmProvEnum.DES);
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        var OldPasswords = from pwd in dc.UserInfos
                           where pwd.User_id.Equals((int)Session["UserID"])
                           select new
                           {
                               密码 = pwd.Password
                           };

        System.Text.RegularExpressions.Regex pattern = new System.Text.RegularExpressions.Regex("^[A-Za-z0-9]+$");

        if (!RepeatPassword.Text.Trim().Equals(NewPassword.Text.Trim()))
        {
            JavaScript.JSDialog.Alert(this, "确认密码与新密码不一致");
        }
        else if ((NewPassword.Text.Trim().Length < 6) || (NewPassword.Text.Trim().Length > 12))
        {
            JavaScript.JSDialog.Alert(this, "密码长度必须在6-12位");
        }
        else if (!pattern.Match(NewPassword.Text.Trim()).Success)
        {
            JavaScript.JSDialog.Alert(this, "密码只能包含0-9数字，a-zA-Z字母");
        }
        else if (!sc.Decrypting(OldPasswords.ToArray().ElementAt(0).密码, "binhai").Equals(OldPassword.Text.Trim()))
        {
           // JavaScript.JSDialog.Alert(this, sc.Decrypting(OldPasswords.ToArray().ElementAt(0).密码, "binhai") + "      " + OldPassword.Text.Trim());

            JavaScript.JSDialog.Alert(this, "原始密码输入不正确");
        }
        else
        {
            var MotifiyingPasswords = from pwd in dc.UserInfos
                                      where pwd.User_id.Equals((int)Session["UserID"])
                                      select pwd;

            MotifiyingPasswords.ToArray().ElementAt(0).Password = sc.Encrypting(NewPassword.Text.Trim(), "binhai");

            try
            {
                dc.SubmitChanges();
            }
            catch (Exception ex)
            {

            }
            JavaScript.JSDialog.Alert(this, "修改成功");
        }
        dc.Dispose();


    }
}
